require 'rubygems'
gem 'soap4r'
require 'soap/wsdlDriver'
require 'soap/header/simplehandler'
require 'defaultDriver.rb'
require 'authStub.rb'
require 'WSHelpers.rb'

props = {
  :object => 'DataExtensionField',
  :props => ['Ordinal','ObjectID','DataExtension.CustomerKey']
}

filter = {
  :filterType => 'Complex',
  :filter => [['CustomerKey','like','APITestDe'],'and',['Name','equals','FirstName']]
}

deRet = BuildRetrieve.new()
des = deRet.creates(props,filter)

objID = []
des.results.each do |result|
  objID.push(result.objectID)
end

#Async update options
#options = CreateOptions.new(nil,nil,nil,nil,nil,nil,nil,nil,'Asynchronous')
#Upsert update options
options = UpdateOptions.new(nil,nil,SaveOption.new('DataExtensionField','UpdateAdd'),nil,nil,nil,nil,nil,nil,nil,nil,nil)
#options = nil

#Data Extension Update
de = []
date = Date.today.strftime("%m/%d/%Y")
5.times do |i|
  deName = "APITestDe#{i}"
  deDesc = deName + ' Created through the API ' + date
  deField = DataExtensionField.new(nil,nil,nil,nil,nil,nil,objID[i],nil,nil,nil,'FirstName',nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,"Our Valued Subscriber #{i}",nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil)
  de.push(DataExtension.new(nil,nil,nil,nil,nil,nil,nil,deName,nil,nil,deName,deDesc,false,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,[deField],nil,nil,nil))
end
apiObj = de

resp = $driver.update(UpdateRequest.new(options,[*apiObj]))